﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CarHelpDesk.Web.Utils;
using System.Web.Security;


namespace CarHelpDesk.Web.Models.Repositories.LinqRepositories
{
    public class UserDetailsRepository : IUserDetailsRepository
    {
        #region Constants

        //TODO: store constants in DB
        public static readonly int USER_MIN_RATING = 0;
        public static readonly int JUNIOR_MODERATOR_MIN_RATING = 10;
        public static readonly int MODERATOR_MIN_RATING = 20;
        public static readonly int MEGA_MODERATOR_MIN_RATING = 30;


        #endregion


        #region Fields

        private LinqModelDataContext _db;

        private static Dictionary<string, int> roleMinRatings;

        #endregion

        #region Constructors

        static UserDetailsRepository()
        {
            UserDetailsRepository.roleMinRatings = new Dictionary<string,int>();

            roleMinRatings.Add(RoleNames.UserRole, UserDetailsRepository.USER_MIN_RATING);
            roleMinRatings.Add(RoleNames.JuniorModeratorRole, UserDetailsRepository.JUNIOR_MODERATOR_MIN_RATING);
            roleMinRatings.Add(RoleNames.ModeratorRole, UserDetailsRepository.MODERATOR_MIN_RATING);
            roleMinRatings.Add(RoleNames.MegaModeratorRole, UserDetailsRepository.MEGA_MODERATOR_MIN_RATING);

        }

        public UserDetailsRepository()
        {
            _db = new LinqModelDataContext();
#if DEBUG
            _db.Log = DebugTextWriter.Instance;
#endif
        }

        internal UserDetailsRepository(LinqModelDataContext db)
        {
            this._db = db;
        }

        #endregion

        #region IUserDetailsRepository Members

        public int getUserRatingByID(Guid userID)
        {
            var ratings = from p in _db.UserDetails
                          where p.UserID == userID
                          select p.UserRating;
            
            return ratings.SingleOrDefault(); 
        }

        /// <summary>
        /// Gets the user detail by GUID.
        /// </summary>
        /// <param name="userGuid">The user's GUID.</param>
        /// <returns></returns>
        public UserDetail getUserDetailByGuid(Guid userGuid)
        {
            var details = from p in _db.UserDetails
                          where p.UserID == userGuid
                          select p;

            return details.SingleOrDefault(); 
        }

        /// <summary>
        /// Updates the user's details.
        /// </summary>
        /// <param name="userDetail">The user detail.</param>
        public void updateUserDetail(UserDetail userDetail)
        {
            _db.SubmitChanges();
        }

        /// <summary>
        /// Adds the user detail.
        /// </summary>
        /// <param name="newUserDetails">The new user details.</param>
        public void addUserDetail(UserDetail newUserDetails)
        {
            _db.UserDetails.InsertOnSubmit(newUserDetails);
            _db.SubmitChanges();
        }

        public void addToUserRating(aspnet_User user, int value)
        {
            user.UserDetail.UserRating += value;
            _db.SubmitChanges();

            //throws "Network access for Distributed Transaction Manager (MSDTC) has been disabled."
            //Uncoment roles when distiributed transacitons are enabled in DB server

            //foreach (var item in UserDetailsRepository.roleMinRatings)
            //{
            //    if (user.UserDetail.UserRating >= item.Value && !Roles.IsUserInRole(user.UserName, item.Key))
            //    {
            //        Roles.AddUserToRole(user.UserName, item.Key);
            //    }
            //}
        }

        #endregion
    }
}
